package com.css.fxfzdpzs.modules.aqbz.service.impl;

import com.css.fxfzdpzs.modules.aqbz.entity.first.*;
import com.css.fxfzdpzs.modules.aqbz.entity.second.*;
import com.css.fxfzdpzs.modules.aqbz.repository.AqbzRepository;
import com.css.fxfzdpzs.modules.aqbz.service.AqbzService;
import com.css.fxfzdpzs.modules.aqbz.vo.AbMapDateEntity;
import com.css.fxfzdpzs.modules.aqbz.vo.AbMapDateVO;
import com.css.fxfzdpzs.modules.aqbz.vo.AqbzSecondVO;
import com.css.fxfzdpzs.modules.aqbz.vo.AqbzSysUsageSituationVO;
import com.css.fxfzdpzs.util.PlatformObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author y1fan
 * @description
 */
@Service
public class AqbzServiceImpl implements AqbzService {

    @Resource
    AqbzRepository aqbzRepository;

    @Override
    public AqbzSysUsageSituationVO getActivityMap() {
        AqbzSysUsageSituationVO vo=new AqbzSysUsageSituationVO();
        List<AbActivityEvent> activityEvent = aqbzRepository.getActivityEvent();
        List<AbActivityEvent> activityEvent0 = new ArrayList<>();
        List<AbActivityEvent> activityEvent1 = new ArrayList<>();
        if (PlatformObjectUtils.isNotEmpty(activityEvent)){

            for (AbActivityEvent abActivityEvent : activityEvent) {
                if ("0".equals(abActivityEvent.getHdlb())){
                    activityEvent0.add(abActivityEvent);
                }
                if ("1".equals(abActivityEvent.getHdlb())){
                    activityEvent1.add(abActivityEvent);
                }
            }
            Collections.sort(activityEvent0, (o1, o2) -> o2.getIaTime().compareTo(o1.getIaTime()));
            //如果排序后，需要取前几条，可以直接使用sublist(startIndex , endIndex)
            Collections.sort(activityEvent1, (o1, o2) -> o2.getIaTime().compareTo(o1.getIaTime()));
            //如果排序后，需要取前几条，可以直接使用sublist(startIndex , endIndex)

        }
        vo.setAbActivityEvents0(activityEvent0);
        vo.setAbActivityEvents1(activityEvent1);
        List<AbActivityNumStatistics> abActivityNumStatistics=aqbzRepository.getAbActivityNumStatistic();
        vo.setAbActivityNumStatistics(abActivityNumStatistics);
        //重点场所次数
       // List<AbPlaceHotList> abPlaceHotLists=aqbzRepository.getPlaceHotList();
        List<AbPlaceHotList> abPlaceHotLists=new ArrayList<>();
        List<String> siteList=aqbzRepository.getSiteName();
        if (PlatformObjectUtils.isNotEmpty(siteList)){
            for (String siteName : siteList) {
                AbPlaceHotList abPlaceHotList =new AbPlaceHotList();
                abPlaceHotList.setKeyLocations(siteName);
                abPlaceHotList.setPlaceNum(aqbzRepository.getPlaceNumBySiteName(siteName));
                abPlaceHotLists.add(abPlaceHotList);
            }
        }
        Collections.sort(abPlaceHotLists, (o1, o2) -> o2.getPlaceNum().compareTo(o1.getPlaceNum()));
        vo.setAbPlaceHotList(abPlaceHotLists);
        List<AbProvinceActivationRank> abProvinceActivationRanks=aqbzRepository.getAbProvinecActivationRank();
        Collections.sort(abProvinceActivationRanks, (o1, o2) -> o2.getAccumulateAccessNum().compareTo(o1.getAccumulateAccessNum()));
        vo.setAbProvinceActivationRank(abProvinceActivationRanks);
        AbSystemAchievementsStatistics abSystemAchievementsStatistics=aqbzRepository.getAbSystemAchievementsStatistics();
        vo.setAbSystemAchievementsStatistics(abSystemAchievementsStatistics);
        AbSystemYseSituation abSystemYseSituations=aqbzRepository.getAbSystemYseSituation();
        vo.setAbSystemYseSituation(abSystemYseSituations);
        return vo;
    }

    @Override
    public AqbzSecondVO getSecond(String iaId) {
        AqbzSecondVO vo=new AqbzSecondVO();
        vo.setIaId(iaId);
        List<AbRiskDangerPoints> abRiskDangerPoints = aqbzRepository.getAbRiskDangerPoints(iaId);
        if (PlatformObjectUtils.isEmpty(abRiskDangerPoints)){
            return null;
        }
        AbRiskProportionStatistics poHuai=new AbRiskProportionStatistics();
        AbRiskProportionStatistics huiHuai=new AbRiskProportionStatistics();
        List<AbRiskProportionStatistics> list=new ArrayList<>();
        if (abRiskDangerPoints.size()==0){
            poHuai.setBreakResultsType("3");
            poHuai.setBreakResultsProportion("0");
            huiHuai.setBreakResultsType("3");
            huiHuai.setBreakResultsProportion("0");
            list.add(poHuai);
            list.add(huiHuai);
        }else {
            int[] numbers = new int[abRiskDangerPoints.size()];
            for (int i = 0; i < abRiskDangerPoints.size(); i++) {
                AbRiskDangerPoints entity = abRiskDangerPoints.get(i);
                //最大破坏类型 0表示基本破坏 1轻微破坏 2中等破坏 3严重破坏 4损坏
                numbers[i] =Integer.parseInt(entity.getStructureType());
            }
            //3严重破坏
            String three = calculatePercentage(numbers, 3);
            //4损坏
            String four = calculatePercentage(numbers, 4);
            poHuai.setBreakResultsType("3");
            poHuai.setBreakResultsProportion(three);
            huiHuai.setBreakResultsType("4");
            huiHuai.setBreakResultsProportion(four);
            list.add(poHuai);
            list.add(huiHuai);
        }
        vo.setAbRiskProportionStatistics(list);
        vo.setAbRiskDangerPoints(abRiskDangerPoints);
        vo.setReport(aqbzRepository.getReport(iaId));
        vo.setTjzs(aqbzRepository.getTjzs(iaId));
        return vo;
    }

    @Override
    public List<AbRiskDangerPoints> getDangerPoints(String iaId) {
        return aqbzRepository.getAbRiskDangerPoints(iaId);
    }

    @Override
    public List<AbRiskProportionStatistics> getProportionStatistics(String iaId) {
        List<AbRiskDangerPoints> abRiskDangerPoints = aqbzRepository.getAbRiskDangerPoints(iaId);
        if (PlatformObjectUtils.isEmpty(abRiskDangerPoints)){
            return null;
        }
        AbRiskProportionStatistics poHuai=new AbRiskProportionStatistics();
        AbRiskProportionStatistics huiHuai=new AbRiskProportionStatistics();
        List<AbRiskProportionStatistics> list=new ArrayList<>();
        if (abRiskDangerPoints.size()==0){
            poHuai.setBreakResultsType("3");
            poHuai.setBreakResultsProportion("0");
            huiHuai.setBreakResultsType("3");
            huiHuai.setBreakResultsProportion("0");
            list.add(poHuai);
            list.add(huiHuai);
        }else {
            int[] numbers = new int[abRiskDangerPoints.size()];
            for (int i = 0; i < abRiskDangerPoints.size(); i++) {
                AbRiskDangerPoints entity = abRiskDangerPoints.get(i);
                //最大破坏类型 0表示基本破坏 1轻微破坏 2中等破坏 3严重破坏 4损坏
                numbers[i] =Integer.parseInt(entity.getStructureType());
            }
            //3严重破坏
            String three = calculatePercentage(numbers, 3);
            //4损坏
            String four = calculatePercentage(numbers, 4);
            poHuai.setBreakResultsType("3");
            poHuai.setBreakResultsProportion(three);
            huiHuai.setBreakResultsType("4");
            huiHuai.setBreakResultsProportion(four);
            list.add(poHuai);
            list.add(huiHuai);
        }
       return list;
    }

    @Override
    public List<AbTujian> getTjzs(String iaId) {
        return aqbzRepository.getTjzs(iaId);
    }

    @Override
    public AbReport getReport(String iaId) {
        return aqbzRepository.getReport(iaId);
    }

    @Override
    public List<AbKeySite> getZdcs(String iaId) {
        List<AbKeySite> zdcs = aqbzRepository.getZdcs(iaId);
        Integer i=1;
        if (PlatformObjectUtils.isNotEmpty(zdcs)){
            for (AbKeySite zdc : zdcs) {
                zdc.setNum(i);
                i++;
            }
        }

        return zdcs;
    }

    @Override
    public List<AbLittleZdcs> getLittleZdcs(String zdcsId) {
        return aqbzRepository.getLittleZdcs(zdcsId);
    }

    @Override
    public AbMapDateVO getMapDate() {
        AbMapDateVO vo =new AbMapDateVO();
        List<AbMapDateEntity> country= aqbzRepository.getMapDate("0");
        List<AbMapDateEntity> province= aqbzRepository.getMapDate("1");
        vo.setCountry(country);
        vo.setProvince(province);
        return vo;
    }

    @Override
    public List<AbMapDate> getIa(String provinceCode, String type) {
        return aqbzRepository.getIa(provinceCode,type);
    }


    public static String calculatePercentage(int[] numbers, int target) {
        int count = 0;
        int total = numbers.length;

        for (int number : numbers) {
            if (number == target) {
                count++;
            }
        }

        double percentage = (double) count / total * 100;
        // 格式化百分比为两位小数
        DecimalFormat df = new DecimalFormat("0.00");
        String formattedPercentage = df.format(percentage);
        return formattedPercentage;
    }
}
